package com.sk.rpcAssist.io.protocol;

import com.alibaba.fastjson.JSON;
import lombok.Data;

import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Objects;

/**
 * description：请求参数method类型封装
 *
 * @author 孙文斌
 * @Date: 2021/1/25 18:11
 */
@Data
public class RpcRequest implements Serializable {

    private static final long serialVersionUID = -1755822586579277552L;
    String className;
    String methodName;
    Object[] arguments;
    Class<?>[] parameterTypes;

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        RpcRequest that = (RpcRequest) o;
        return Objects.equals(className, that.className) &&
                Objects.equals(methodName, that.methodName) &&
                Arrays.equals(arguments, that.arguments) &&
                Arrays.equals(parameterTypes, that.parameterTypes);
    }

    @Override
    public int hashCode() {
        //todo:优化，避免hash算法碰撞
        //todo:保证属性值相同的对象hashcode值相同，不相等的要避免碰撞
        int result = Objects.hash(className, methodName);
        result = 31 * result + Arrays.hashCode(arguments);
        result = 31 * result + Arrays.hashCode(parameterTypes);
        return result;
    }
}
